package com.rocksea.app.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.rocksea.app.BaseFragment;
import com.rocksea.app.R;
import com.rocksea.app.db.DbCenter;
import com.rocksea.app.db.SonicWave.SonicWave;
import com.rocksea.app.event.UpDateSonicWave;
import com.rocksea.app.tasks.Temp;
import com.rocksea.app.weight.SonicWaveImage;
import com.rocksea.app.weight.SonicWaveView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;
import butterknife.OnClick;

/**
 * @author Administrator
 * @date 2017/11/24
 */

public class SetionViewFragment extends BaseFragment
{

    public static final String VALID_KEY = "VALID_KEY";

    @InjectView(R.id.SonicWaveView)
    SonicWaveView mSonicWaveView;
    private String sectionId;
    private int isValid;

    @OnClick(R.id.Edit)
    public void changeState()
    {
        switch (mSonicWaveView.getCurrentPattern())
        {
            case SonicWaveView.PATTERN_EDIT:
                mSonicWaveView.setPattern(SonicWaveView.PATTERN_NORMAL);
                break;
            case SonicWaveView.PATTERN_NORMAL:
                mSonicWaveView.setPattern(SonicWaveView.PATTERN_EDIT);
                break;
        }
    }

    public static SetionViewFragment newInstance(Bundle args)
    {
        SetionViewFragment fragment = new SetionViewFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView()
    {
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
        sectionId = getArguments().getString(SetionInfoFragment.SECTION_UUID);
        isValid = getArguments().getInt(VALID_KEY);
        mSonicWaveView.setOnItemValidChangedListener(new SonicWaveView.OnItemValidChangedListener()
        {
            @Override
            public void onChanged(String uuid, boolean isValid)
            {
                DbCenter.makeScWaveAsIsValid(uuid,isValid?0:1);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onNotificationList(UpDateSonicWave upDateSonicWave)
    {
        if (Temp.getSonicWave()!=null)
        {
            ArrayList<SonicWave.SonicWaveData> fromMenSonicWave = new ArrayList<>();
            for (int i = 0; i < Temp.getSonicWave().SonicWaveDatas.size(); i++)
            {
                if (Temp.getSonicWave().SonicWaveDatas.get(i).sectionId.equals(sectionId))
                    fromMenSonicWave.add(Temp.getSonicWave().SonicWaveDatas.get(i));
            }
            handSonicWaveData(fromMenSonicWave);
        }
    }
    @Override
    protected void initData() {
        loading();
        AsyQueryWaveDataTask task = new AsyQueryWaveDataTask(sectionId);
        task.execute();
    }

    private void handSonicWaveData(List<SonicWave.SonicWaveData> sonicWaveDatas)
    {
        if(sonicWaveDatas==null)
        {
            loadFailed();
            return;
        }
        ArrayList<SonicWave.SonicWaveData> temp = new ArrayList<>();
        if (isValid==0)
        {
            for (int i = 0; i < sonicWaveDatas.size(); i++)
            {
                if (sonicWaveDatas.get(i).isValid==0)
                    temp.add(sonicWaveDatas.get(i));
            }
        }
        else if (isValid==1)
        {
            for (int i = 0; i < sonicWaveDatas.size(); i++)
            {
                if (sonicWaveDatas.get(i).isValid==1)
                    temp.add(sonicWaveDatas.get(i));
            }
        }
        if (temp.size()==0)
        {
            loadFailed();
            mErrorText.setText("未找到数据");
            return;
        }
        mSonicWaveView.setSonicWaveData(temp);
        loadCompleted();

    }

    @Override
    protected void requestAgain()
    {
        initData();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.section_valid_fragment_layout;
    }

    public class AsyQueryWaveDataTask extends AsyncTask<Void,Void, List<SonicWave.SonicWaveData>>
    {

        private String baseInfoId;

        private AsyQueryWaveDataTask(String baseInfoId)
        {
            this.baseInfoId = baseInfoId;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        @Override
        protected List<SonicWave.SonicWaveData> doInBackground(Void... params)
        {
            if (Temp.getSonicWave()!=null)
            {
                ArrayList<SonicWave.SonicWaveData> fromMenSonicWave = new ArrayList<>();
                for (int i = 0; i < Temp.getSonicWave().SonicWaveDatas.size(); i++)
                {
                    if (Temp.getSonicWave().SonicWaveDatas.get(i).sectionId.equals(sectionId))
                    {
                        fromMenSonicWave.add(Temp.getSonicWave().SonicWaveDatas.get(i));
                    }
                }
                return fromMenSonicWave;
            }
            loading();
            return DbCenter.mScDbHelper.querySonicWaveData(baseInfoId);
        }
        @Override
        protected void onPostExecute(List<SonicWave.SonicWaveData> sonicWaveDatas)
        {
            super.onPostExecute(sonicWaveDatas);
            handSonicWaveData(sonicWaveDatas);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
